﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

using RRRSRoguelike.Entities;
using RRRSRoguelike.Enums;
using RRRSRoguelike.Managers;

namespace RRRSRoguelike.Helpers.Behaviors
{
    public class Lurker
    {
        public static void MoveToTarget(Monster caller,Point target)
        {
            Point temp;
            IList<Monster> Monsters = DungeonManager.Instance.CreatureManager.Monsters;
			caller.hasMoved = false;

			caller.CalculateMoves(target);
			
			caller.Moves = caller.Moves.OrderBy(m=> m.Value).ToDictionary(p=>p.Key,p=>p.Value);
            
            for (int i=0;i<caller.Moves.Count();i++)
            {
            	temp = Dungeon.AddPoints(caller.Position,
				DungeonManager.Instance.ActionManager.DirectionVectors.ElementAt(
            	                         	(int)caller.Moves.ElementAt(i).Key).Value);
            	
            	temp = DungeonLevel.ClampToDungeon(temp,false);
            	
            	if (DungeonManager.Instance.DungeonLevel.IsOKToMove(temp) &&
            		!Monsters.Any(m => m.X == temp.X && m.Y == temp.Y))
            	{
                	caller.hasMoved = true;
                	caller.Position = temp;
                	return;
            	}
            }
			caller.hasMoved = true;            
			
        }
    }
}
